home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 10 / Engine / ViewFrustum.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-10-01  |  4.2 KB  |  113 lines

  1. //-----------------------------------------------------------------------------
  2. // ViewFrustum.h implementation.
  3. // Refer to the ViewFrustum.h interface for more details.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #include "Engine.h"
  9.  
  10. //-----------------------------------------------------------------------------
  11. // Creates a view frustum from the given view matrix.
  12. //-----------------------------------------------------------------------------
  13. void ViewFrustum::Update( D3DXMATRIX *view )
  14. {
  15.     // Calculate the field of view.
  16.     D3DXMATRIX fov;
  17.     D3DXMatrixMultiply( &fov, view, &m_projection );
  18.  
  19.     // Calculate the right plane.
  20.     m_planes[0].a = fov._14 - fov._11;
  21.     m_planes[0].b = fov._24 - fov._21;
  22.     m_planes[0].c = fov._34 - fov._31;
  23.     m_planes[0].d = fov._44 - fov._41;
  24.  
  25.     // Calculate the left plane.
  26.     m_planes[1].a = fov._14 + fov._11;
  27.     m_planes[1].b = fov._24 + fov._21;
  28.     m_planes[1].c = fov._34 + fov._31;
  29.     m_planes[1].d = fov._44 + fov._41;
  30.  
  31.     // Calculate the top plane.
  32.     m_planes[2].a = fov._14 - fov._12;
  33.     m_planes[2].b = fov._24 - fov._22;
  34.     m_planes[2].c = fov._34 - fov._32;
  35.     m_planes[2].d = fov._44 - fov._42;
  36.  
  37.     // Calculate the bottom plane.
  38.     m_planes[3].a = fov._14 + fov._12;
  39.     m_planes[3].b = fov._24 + fov._22;
  40.     m_planes[3].c = fov._34 + fov._32;
  41.     m_planes[3].d = fov._44 + fov._42;
  42.  
  43.     // Calculate the far plane.
  44.     m_planes[4].a = fov._14 - fov._13;
  45.     m_planes[4].b = fov._24 - fov._23;
  46.     m_planes[4].c = fov._34 - fov._33;
  47.     m_planes[4].d = fov._44 - fov._43;
  48.  
  49.     // Normalize the planes.
  50.     D3DXPlaneNormalize( &m_planes[0], &m_planes[0] );
  51.     D3DXPlaneNormalize( &m_planes[1], &m_planes[1] );
  52.     D3DXPlaneNormalize( &m_planes[2], &m_planes[2] );
  53.     D3DXPlaneNormalize( &m_planes[3], &m_planes[3] );
  54.     D3DXPlaneNormalize( &m_planes[4], &m_planes[4] );
  55. }
  56.  
  57. //-----------------------------------------------------------------------------
  58. // Set's the view frustum's internal projection matrix.
  59. //-----------------------------------------------------------------------------
  60. void ViewFrustum::SetProjectionMatrix( D3DXMATRIX projection )
  61. {
  62.     m_projection = projection;
  63. }
  64.  
  65. //-----------------------------------------------------------------------------
  66. // Returns true if the given box is inside the view frustum.
  67. //-----------------------------------------------------------------------------
  68. bool ViewFrustum::IsBoxInside( D3DXVECTOR3 min, D3DXVECTOR3 max )
  69. {
  70.     for( char p = 0; p < 5; p++ )
  71.     {
  72.         if( D3DXPlaneDotCoord( &m_planes[p], &D3DXVECTOR3( min.x, min.y, min.z ) ) >= 0.0f )
  73.             continue;
  74.         if( D3DXPlaneDotCoord( &m_planes[p], &D3DXVECTOR3( max.x, min.y, min.z ) ) >= 0.0f )
  75.             continue;
  76.         if( D3DXPlaneDotCoord( &m_planes[p], &D3DXVECTOR3( min.x, max.y, min.z ) ) >= 0.0f )
  77.             continue;
  78.         if( D3DXPlaneDotCoord( &m_planes[p], &D3DXVECTOR3( max.x, max.y, min.z ) ) >= 0.0f )
  79.             continue;
  80.         if( D3DXPlaneDotCoord( &m_planes[p], &D3DXVECTOR3( min.x, min.y, max.z ) ) >= 0.0f )
  81.             continue;
  82.         if( D3DXPlaneDotCoord( &m_planes[p], &D3DXVECTOR3( max.x, min.y, max.z ) ) >= 0.0f )
  83.             continue;
  84.         if( D3DXPlaneDotCoord( &m_planes[p], &D3DXVECTOR3( min.x, max.y, max.z ) ) >= 0.0f )
  85.             continue;
  86.         if( D3DXPlaneDotCoord( &m_planes[p], &D3DXVECTOR3( max.x, max.y, max.z ) ) >= 0.0f )
  87.             continue;
  88.  
  89.         return false;
  90.     }
  91.  
  92.     return true;
  93. }
  94.  
  95. //-----------------------------------------------------------------------------
  96. // Returns true if the given box is inside the view frustum.
  97. //-----------------------------------------------------------------------------
  98. bool ViewFrustum::IsBoxInside( D3DXVECTOR3 translation, D3DXVECTOR3 min, D3DXVECTOR3 max )
  99. {
  100.     return IsBoxInside( min + translation, max + translation );
  101. }
  102.  
  103. //-----------------------------------------------------------------------------
  104. // Returns true if the given sphere is inside the view frustum.
  105. //-----------------------------------------------------------------------------
  106. bool ViewFrustum::IsSphereInside( D3DXVECTOR3 translation, float radius )
  107. {
  108.     for( char p = 0; p < 5; p++ )
  109.         if( D3DXPlaneDotCoord( &m_planes[p], &translation ) < -radius )
  110.             return false;
  111.  
  112.     return true;
  113. }